package com.gaga.console.core.controller.sys;


import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gaga.common.enums.DelFlag;
import com.gaga.common.path.ApiPath;
import com.gaga.common.result.Result;
import com.gaga.console.base.controller.BaseController;
import com.gaga.console.base.entity.PageParams;
import com.gaga.console.base.mybatisplus.LambdaQueryWrapperExt;
import com.gaga.console.core.entity.SysDatabase;
import com.gaga.console.core.service.SysDatabaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 前端控制器
 *
 * @author liujia
 * @since 2022-06-01
 */
@Api(tags = "系统模块-数据源管理")
@RestController
@RequestMapping(ApiPath.ROOT + "/sys/database")
public class SysDatabaseController extends BaseController {
    @Autowired
    private SysDatabaseService sysDatabaseService;

    /**
     * 获取列表
     */
    @ApiOperation(value = "查询list")
    @PostMapping("/list")
    public Result list(@RequestBody PageParams<SysDatabase> data) {
        LambdaQueryWrapperExt<SysDatabase> wraps = new LambdaQueryWrapperExt<>();
        wraps.eq(SysDatabase::getDelFlag, DelFlag.OK.getCode());
        data.buildSpecWrap(wraps);

        IPage<SysDatabase> page = sysDatabaseService.page(data.buildPage(), wraps);
        return Result.ok(data.buildTableResult(page));
    }

    @ApiOperation(value = "查询所有list")
    @GetMapping("/allList")
    public Result allList() {
        LambdaQueryWrapperExt<SysDatabase> wraps = new LambdaQueryWrapperExt<>();
        wraps.eq(SysDatabase::getDelFlag, DelFlag.OK.getCode());
        return Result.ok(sysDatabaseService.list(wraps));
    }

    @ApiOperation(value = "查询所有table")
    @PostMapping("/tableListBySchema")
    public Result getTableListBySchema(@RequestBody PageParams<SysDatabase> data) {
        return Result.ok(sysDatabaseService.getTableListBySchema(data));
    }

    @ApiOperation(value = "查询所有table")
    @PostMapping("/tableList/{id}")
    public Result getTableList(@PathVariable Long id) {
        return Result.ok(sysDatabaseService.getTableList(id));
    }


    @ApiOperation(value = "查询所有schema")
    @PostMapping("/schemaList/{id}")
    public Result getSchemaList(@PathVariable Long id) {
        return Result.ok(sysDatabaseService.getSchemaList(id));
    }

    @ApiOperation(value = "查询所有schema和table")
    @PostMapping("/schemaAndTableList/{id}")
    public Result getSchemaAndTableList(@PathVariable Long id) {

        return Result.ok(sysDatabaseService.getSchemaAndTableList(id));
    }


    /**
     * 测试连接
     */
    @ApiOperation(value = "testConnection")
    @GetMapping("/testConnection/{id}")
    public Result testConnection(@PathVariable Long id) {
        return Result.ok(sysDatabaseService.testConnection(id));
    }


    /**
     * 根据详细信息
     */
    @ApiOperation(value = "获取单个记录")
    @GetMapping(value = "/{id}")
    public Result getEntity(@PathVariable Long id) {
        return Result.ok(sysDatabaseService.getById(id));
    }

    /**
     * 保存
     */
    @ApiOperation(value = "保存记录")
    @PostMapping("/save")
    public Result save(@Validated @RequestBody SysDatabase entity) {
        return toAjaxBoolean(sysDatabaseService.saveOrUpdate(entity));
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除记录")
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids) {

        if (ArrayUtil.isEmpty(ids)) {
            return toAjaxBoolean(false);
        }

        List<SysDatabase> removeEntityList = Arrays.stream(ids)
                .map(id -> {
                    SysDatabase entity = new SysDatabase().setId(id);
                    entity.setDelFlag(DelFlag.DELETED.getCode());
                    entity.setDeleteTime(new Date());
                    return entity;
                }).collect(Collectors.toList());

        return toAjaxBoolean(sysDatabaseService.updateBatchById(removeEntityList));
    }
}
